package com.zhongxing.hdy.spritel_hdy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.zhongxing.hdy.spritel_hdy.util.ToastUtil;

/**
 * Created by Administrator on 2017/6/29.
 */

public class LoginActivity extends Activity {

    private Button btn_login;
    private Button btn_logout;
    private String username;
    private String password;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initUI();
    }

    /**
     * 初始化控件绑定
     */
    private void initUI(){
        btn_login= (Button) findViewById(R.id.btn_login_p);
        btn_logout= (Button) findViewById(R.id.btn_logout_p);

        //添加点击事件
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editText= (EditText) findViewById(R.id.username);
                username= editText.getText().toString().trim();
                editText= (EditText) findViewById(R.id.password);
                password=editText.getText().toString().trim();
                String message = "";
                if ("admin".equals(username)&&"123".equals(password)){
                    //跳转主界面
                    Intent intent=new Intent();//创建意图
                    //设置出发点 出发点为content类型，写getApplicationContext()，终点为XXX.class
                    intent.setClass(getApplicationContext(),MainActivity.class);
                    //执行意图
                    startActivity(intent);
                    return;
                }else if(null==username || "".equals(username)){
                    message = "账户名不能为空";
                }else if(null==password || "".equals(password)){
                    message = "密码不能为空";
                }else if(username.length()<5){
                    message = "账户名长度不能小于5";
                }else if(password.length()<3){
                    message = "密码长度不能小于5";
                }else {
                    message = "账户名或密码不能正确";
                }
                ToastUtil.toast(getApplicationContext(),message);
                //Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
            }
        }); //代理模式
        btn_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * Toast : 吐司 ， 会自动消失
                 *      makeText(Context context, CharSequence text, @Duration int duration)
                 *          context;上下文
                 *          text：显示文本
                 *          duration：持续时间
                 */
                ToastUtil.toast(getApplicationContext(),"登出");
                //Toast.makeText(getApplicationContext(),"登出",Toast.LENGTH_SHORT).show();
            }
        });
    }


}
